import { Component, OnInit } from "@angular/core";
import { DataReportService } from "../../service/http/date-report.service";

@Component({
  selector: "app-tab",
  templateUrl: "./tab.component.html",
  styleUrls: ["./tab.component.less"],
})
export class TabComponent implements OnInit {
  listData: any[] = [];
  listTotal: number = 0;
  listParams = {
    listData: [],
    listTotal: 0,
  };
  formParams = {
    pageNum: 1,
    pageSize: 10,
  };
  constructor(private instructionHttpService: DataReportService) {}

  ngOnInit(): void {
    this.getFirstList();
  }

  getFirstList(data?: any) {
    let params = {
      ...data,
    };
    this.instructionHttpService.getInstructionListFirst(params).subscribe(
      (res: any) => {
        if (res.status === 200) {
          this.listParams.listData = res.data.list;
          this.listParams.listTotal = res.data.total;
          this.listParams.listData.forEach((item: any) => {
            if (item.statisticsTotal && item.statisticsFail) {
              item.status = `总数${item.statisticsTotal}，失败${item.statisticsFail}`;
            }
            item.expand = false;
          });
        }
      },
      (err: any) => {
        console.log(err);
      },
    );
  }

  getSecondList(item: any) {
    let params = {
      instructionId: item.instructionId,
    };
    this.instructionHttpService.getInstructionListSecond(params).subscribe(
      (res: any) => {
        if (res.status === 200) {
          item.children = res.data;
          item.children.forEach((itemTask: any) => {
            if (itemTask.successNum && itemTask.errorNum) {
              itemTask.taskStatistics = `总数${itemTask.successNum + itemTask.errorNum}，失败${itemTask.errorNum}`;
            }
          });
        }
      },
      (err: any) => {
        console.log(err);
      },
    );
  }

  getList(params: any) {
    this.formParams = { ...this.formParams, ...params };
    this.getFirstList(this.formParams);
  }
}
